package com.leesche.zcsp.node.app.v1.controller.operate;

import com.leesche.zcsp.node.app.v1.controller.admin.AppAdminController;
import com.leesche.zcsp.node.utils.UUIDGenerateUtil;
import com.leesche.zcsp.node.utils.UtilDate;
import com.leesche.zcsp.node.utils.web.PagingCondition;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDiscountCode;
import com.leesche.zcsp.node.web.service.PubDiscountCodeService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDiscountCountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountCodeDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCodeCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/11/21.
 * 设备优惠码
 */
@Controller
@RequestMapping("/app/v1/operate/coupon")
public class AppCouponController extends AppAdminController {
    private final static Logger LOGGER = LoggerFactory.getLogger(AppCouponController.class);

    @Autowired
    private PubDiscountCodeService codeService;

    /**
     * 设备优惠码列表
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listView", method = RequestMethod.POST)
    public Map<String,Object> listView(
            QueryPubDiscountCodeCondition condition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            condition.setAdminId(adminId);
            List<PubDiscountCodeDto> discountCodeDtoList = codeService.findListsByCondition(condition);
            List<InfoDiscountCountDto> countDtoList = codeService.selectCodeTypeNum(condition);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("discountCodeDtoList",discountCodeDtoList);
            map.put("countDtoList",countDtoList);
            return WebResult.success().put("data",map).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备优惠码分页查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listPageView", method = RequestMethod.POST)
    public Map<String,Object> listPageView(
        QueryPubDiscountCodeCondition condition
    ){
        try {
            Long adminId = getAdminId();
            condition.setAdminId(adminId);
            PagingResult<PubDiscountCodeDto> discountCodeDtoList = codeService.findAllByPage(condition);
            List<InfoDiscountCountDto> countDtoList = codeService.selectCodeTypeNum(condition);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("discountCodeDtoList",discountCodeDtoList);
            map.put("countDtoList",countDtoList);
            return WebResult.success().put("data",map).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 添加优惠码
     * @param discountCode
     * @param stime 开始时间
     * @param etime 结束时间
     * @param codeCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String,Object> add(
            PubDiscountCode discountCode,
            String stime,
            String etime,
            QueryPubDiscountCodeCondition codeCondition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            discountCode.setAdminid(adminId);
            discountCode.setCodestate(1);
            discountCode.setIntime(UtilDate.strToTime(stime));
            discountCode.setOuttime(UtilDate.strToTime(etime));

            String code;
            codeCondition.setCodeState(1);
            codeCondition.setAdminId(adminId);

            /*Integer codeNum = codeService.selectCodeNum(codeCondition);
            //查询有效优惠码是否超过500
            if(codeNum>500){
                return WebResult.failure("优惠码已超过500个").toMap();
            }else{*/
                for(int i=0;i<discountCode.getIntdata1();i++){
                    String codeTemp = UUIDGenerateUtil.generateShortUuid();
                    codeCondition.setDiscountCode(codeTemp);
                    boolean flag = codeService.selectCheck(codeCondition);

                    if(flag == true){
                        code = UUIDGenerateUtil.generateShortUuid();
                    }else{
                        code = codeTemp;
                    }
                    discountCode.setDiscountcode(code);
                    codeService.insert(discountCode);
                }
                return WebResult.success().toMap();
          /*  }*/

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 使用优惠码
     * @param codeCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Map<String,Object> edit(
            QueryPubDiscountCodeCondition codeCondition
    ){
        try {
            PubDiscountCode discountCode = codeService.selectByModel(codeCondition);

            discountCode.setCodestate(2);
            codeService.updateByPrimaryKeySelective(discountCode);

            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 优惠码各个状态总数
     * @param codeCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/couponTypeNum", method = RequestMethod.POST)
    public Map<String,Object> CouponTypeNum(
            QueryPubDiscountCodeCondition codeCondition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            codeCondition.setAdminId(adminId);
            List<InfoDiscountCountDto> countDtoList = codeService.selectCodeTypeNum(codeCondition);
            return WebResult.success().put("data",countDtoList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 删除优惠码
     * @param discountCodeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchDelCoupon", method = RequestMethod.POST)
    public Map<String,Object> batchDelCoupon(
        Long[] discountCodeId
    ){
        try {
            for(Long couponId : discountCodeId){
                codeService.deleteByPrimaryKey(couponId);
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }





}
